﻿using AccountApi.Entitys;
using AccountApi.Models;
using AccountApi.tools;
using LitJson;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace AccountApi.Controllers
{
    public class AccountController : ApiController
    {
        // GET: api/Account
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/Account/5
        public AccountEntity Get(int id)
        {
            return AccountDBModel.Instance.Get(id);
        }

        // POST: api/Account
        public RetValue Post([FromBody]string jsonStr)
        {
            RetValue ret = new RetValue();
            try {
                JsonData jsonData = JsonMapper.ToObject(jsonStr);
                int type = Convert.ToInt32(jsonData["Type"].ToString());
                string userName = jsonData["UserName"].ToString();
                string pwd = jsonData["Pwd"].ToString();
                if (type == 0)
                {
                    //注册
                    ret.RetData = AccountDBModel.Instance.Register(userName, pwd);
                }
                else
                {
                    //登录
                }
            } catch (Exception e) {
                ret.HasError = true;
                ret.ErrorMsg = e.Message;
            }
            
            return ret;
        }

        // PUT: api/Account/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Account/5
        public void Delete(int id)
        {
        }
    }
}
